package com.zeng.vueserve.coreservice.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeng.nicecore.constant.IgnoreConstant;
import com.zeng.nicecore.util.BeanUtil;
import com.zeng.nicecore.util.IgnoreStringUtil;
import com.zeng.vueserve.coreservice.mapper.AuthorityMapper;
import com.zeng.vueserve.coreservice.entity.Authority;
import com.zeng.vueserve.coreapi.vo.AuthorityVo;
import com.zeng.vueserve.coreapi.service.AuthorityService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class AuthorityServiceImpl implements AuthorityService {

    @Autowired
    AuthorityMapper authorityMapper;

    @Override
    public AuthorityVo addOne(AuthorityVo authorityVo) {
    Authority authority = Authority.builder().build();
    BeanUtils.copyProperties(authorityVo, authority, IgnoreStringUtil.ignoreExclude());

    int insert = authorityMapper.insert(authority);

    return BeanUtil.beanToVo(authority, authorityVo);
    }

    @Override
    public AuthorityVo getOneByCode(String code) {

    Authority authority = this.authorityMapper.selectById(code);
    AuthorityVo authorityVo = BeanUtil.beanToVo(authority, new AuthorityVo());

    return authorityVo;
    }

    @Override
    public Integer update(AuthorityVo authorityVo) {
    Authority authority = BeanUtil.VoToBean(authorityVo,
    new Authority(),
    IgnoreStringUtil.ignoreExclude(IgnoreConstant.code.getValue()));
    return this.authorityMapper.updateById(authority);
    }

    @Override
    public Integer delete(String code) {
    return this.authorityMapper.deleteById(code);
    }

    @Override
    public Integer batchDelete(List<String> codes) {

    return this.authorityMapper.deleteBatchIds(codes);
    }

    @Override
    public List<AuthorityVo> findPage(Page page, AuthorityVo authorityVo) {
        Authority authority = BeanUtil.VoToBean(authorityVo, new Authority());
        this.authorityMapper.selectPage(page, this.getWrapper(authorityVo));
        List orders = page.getRecords();
        List<AuthorityVo> authorityVos = new ArrayList<>();
        orders.forEach(e->{
            authorityVos.add(BeanUtil.beanToVo(e, new AuthorityVo()));
        });
        page.setRecords(null);
        return authorityVos;
        }


    @Override
    public Integer count(AuthorityVo authorityVo) {
        Integer integer = this.authorityMapper.selectCount(this.getWrapper(authorityVo));
        return integer;
    }

    public AbstractWrapper getWrapper(AuthorityVo authorityVo) {
        QueryWrapper<Authority> wrapper = new QueryWrapper<>();
        if (authorityVo != null) {


        }
        return wrapper;
    }
}
